<?php
!defined("EW_ROOT") && exit('No direct script access allowed');
/**
* Ecommerce Web Content Management System
* @copyright (c) 2011 ewcms team http://www.ewcms.org
* @license  GNU GENERAL PUBLIC LICENSE (GPLv3)
* @author iceweb
*/
class wsAction
{
	public $data=array();
	public static $plug=array();
	/**
	 * @var wsInput
	 */
	protected $input=NULL;
	
	function __construct()
	{
		require_once EW_ROOT.'source/core/wsForm.class.php';
		if($this->input==NULL)
		{
			$this->input=new wsInput();
		}
	}
	
	/**
	 * 载入视图
	 * @param string $tpl
	 * @param array $array
	 * @param boolean $return
	 */
	protected function loadView($tpl,$array=array(),$return=false)
	{
		$laodTemp=wsTemplate::loadTemplate($tpl,$array,$this->data);
		if($laodTemp===false)
		{
			$tpl=str_replace(PLUG_PATH.'template/','', $tpl);
			wsEcho::showMsg(wsLang::getLang('system_tpl_not_exits').$tpl);
		}
		if($return)
		{
			return $laodTemp;
		}
		else 
		{
			echo $laodTemp;
			if(DEBUG_MODE==0)
			{
				echo $this->showDebugInfo();
			}
			exit;
		}
	}
	
	/**
	 * 显示调试信息
	 * 包含运行时间和当前执行的SQL语句
	 */
	private function showDebugInfo()
	{
		$time=runTime::getSecondTime()-runTime::$time;
		$mem=runTime::getNowMem()-runTime::$memory;
		$info="<div id='runTime' style='border:1px solid #ccc;padding:5px;'>
			runTime:".round($time,4)."s,Memory:".round($mem,2)."KB<br/>".runTime::$sql."
			include file:<br/>".implode("<br/>", get_included_files())."</div>";
		return $info;
	}
	
	/**
	 * 载入成功信息模版
	 * @param string $msg
	 * @param string $url
	 */
	protected function success($msg,$url=FALSE)
	{
		$url=$url===FALSE ?$_SERVER['HTTP_REFERER'] :siteUrl($url);
		$this->loadView("success",array('message'=>$msg,'url'=>$url));
	}
	
	/**
	 * 载入错误处理模版
	 * @param string $msg
	 */
	protected function error($msg='系统发生错误，操作失败!请稍后重试！')
	{
		$this->loadView("error",array('message'=>$msg));
	}
	
	/**
	 * URL跳转
	 * @param string $url
	 */
	protected function redirect($url='')
	{
		if(stripos($url,'http://')===FALSE)
		{
			$url=siteUrl($url);
		}
		header("Location:".$url);
	}
	
	/**
	 * 检测表单字段
	 * @param sting $name
	 * @param string $post
	 * @param string $msg
	 * @param array $checkLen
	 * @param array $oterhFun
	 * @throws Exception
	 */
	protected function checkForm($name,$post,$msg,$checkLen,$oterhFun=false,$ajax=false)
	{
		try 
		{
			return wsForm::formField($name, $post, $msg, $checkLen, $oterhFun,$ajax);
		}
		catch (Exception $e)
		{
			$this->loadView('error',array('message'=>$e->getMessage()));
		}
	}
	
	/**
	 * 表单项目快捷检查方法
	 * @param string $checkType
	 * @param string $name
	 * @param boolean|string $msg
	 * @param int $min
	 * @param int $max
	 * @param string|array|int $post
	 */
	protected function checkFormItem($checkType,$name,$msg=FALSE,$min=1,$max=100,$post="post")
	{
		$msg=$msg ?$msg :wsLang::getLang('form_check_error_lenth');
		$formCheck=import("formCheck",TRUE);
		$checkType=strtolower($checkType);
		switch ($checkType)
		{
			case "int":
				return $this->checkForm($name,$post,$msg,array(wsForm::$int,$min,$max));
				break;
			case "mail":
				return $this->checkForm($name,$post,wsLang::getLang('form_check_error_mail'), array(wsForm::$string,1,200),array($formCheck,'isMail',wsLang::getLang('form_check_error_mail')));; 
				break;
			case "str":
				return $this->checkForm($name,$post,$msg, array(wsForm::$string,$min,$max),array($formCheck,'isString',wsLang::getLang('form_check_error_special')));
				break;
			case "stren":
				return $this->checkForm($name,$post,$msg, array(wsForm::$string,$min,$max),array($formCheck,'isStringEn',wsLang::getLang('form_check_error_special')));
				break;
			default:
				break;
		}
		
	}
}